package com.unknown.wificontrol.ui.home;

import android.view.View;
import android.widget.EditText;

import com.unknown.wificontrol.R;
import com.unknown.wificontrol.http.Http;
import com.unknown.wificontrol.http.HttpListener;
import com.unknown.wificontrol.listener.HXItemSelectListener;
import com.unknown.wificontrol.listener.OnDialogListener;
import com.unknown.wificontrol.ui.BaseActivity;
import com.unknown.wificontrol.util.Preference;
import com.unknown.wificontrol.widget.BottomPopItemView;

import org.json.JSONObject;

import butterknife.BindView;

public class FilterSettingActivity extends BaseActivity {
    @Override
    public int getLayout() {
        return R.layout.layout_filter_setting;
    }

    @Override
    public void initViews() {
        etOperatingMode.setText(Preference.get(this, "OperatingMode", "正常模式"));
        etOperatingMode.setSelection(etOperatingMode.getText().toString().length());
        etSamplingRate.setText(Preference.get(this, "SamplingRate", "5000"));
        etLowPassFilter.setText(Preference.get(this, "LowPassFilter", "17.4"));
        etHighPassFilter.setText(Preference.get(this, "HighPassFilter", "0.84"));
    }

    @BindView(R.id.etOperatingMode)
    EditText etOperatingMode;

    @BindView(R.id.etSamplingRate)
    EditText etSamplingRate;

    @BindView(R.id.etLowPassFilter)
    EditText etLowPassFilter;

    @BindView(R.id.etHighPassFilter)
    EditText etHighPassFilter;


    public void onItem(View v) {
        switch (v.getId()) {
            case R.id.ivOperatingMode: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"正常工作", "软复位", "芯片关断"};
                item.show(etOperatingMode.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etOperatingMode.setText(items[index]);
                        etOperatingMode.setSelection(etOperatingMode.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivSamplingRate: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"20", "40"};
                item.show(etSamplingRate.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etSamplingRate.setText(items[index]);
                        etSamplingRate.setSelection(etSamplingRate.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivLowPassFilter: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"17.4", "13.4", "10", "7"};
                item.show(etLowPassFilter.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etLowPassFilter.setText(items[index]);
                        etLowPassFilter.setSelection(etLowPassFilter.getText().toString().length());
                    }
                });
            }
            break;
            case R.id.ivHighPassFilter: {
                BottomPopItemView item = new BottomPopItemView(mActivity);
                final String[] items = new String[]{"0.84", "3.55", "5.61"};
                item.show(etHighPassFilter.getText().toString(), items, new HXItemSelectListener() {
                    @Override
                    public void onSelected(int index) {
                        etHighPassFilter.setText(items[index]);
                        etHighPassFilter.setSelection(etHighPassFilter.getText().toString().length());
                    }
                });
            }
            break;
        }
    }

    public void onCommit(View v) {
        String OperatingMode = etOperatingMode.getText().toString();
        String SamplingRate = etSamplingRate.getText().toString();
        String LowPassFilter = etLowPassFilter.getText().toString();
        String HighPassFilter = etHighPassFilter.getText().toString();
        if (isEmpty(OperatingMode)) {
            showToast("请输入工作模式");
            return;
        }
        if (isEmpty(SamplingRate)) {
            showToast("请输入采样率");
            return;
        }
        if (isEmpty(LowPassFilter)) {
            showToast("请输入低通滤波");
            return;
        }
        if (isEmpty(HighPassFilter)) {
            showToast("请输入高通滤波");
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("OperatingMode", OperatingMode);
            object.put("SamplingRate", SamplingRate);
            object.put("LowPassFilter", LowPassFilter);
            object.put("HighPassFilter", HighPassFilter);
        } catch (Exception exception) {

        }
        showProgress("设置中...");
        Http.post(getHost() + "/getValues/Filter", object.toString(), new HttpListener() {
            @Override
            public void onReturn(JSONObject jsonObject) {
                hideProgress();
                if (jsonObject != null && jsonObject.length() > 0) {
                    int code = jsonObject.optInt("code");
                    String message = jsonObject.optString("message");
                    if (code == 200) {
                        Preference.save(mActivity, "OperatingMode", OperatingMode);
                        Preference.save(mActivity, "SamplingRate", SamplingRate);
                        Preference.save(mActivity, "LowPassFilter", LowPassFilter);
                        Preference.save(mActivity, "HighPassFilter", HighPassFilter);
                        showSuccess("滤波设置成功", new OnDialogListener(){
                            @Override
                            public void onConfirm() {
                                super.onConfirm();
                                finish();
                            }
                        });
                    } else {
                        showToast(message);
                    }
                } else {
                    showToast("请求失败");
                }

            }
        });
    }
}
